﻿using DelonixRegia.Classes;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace DelonixRegia
{
    public partial class NewStaff : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Account a = (Account)Session["acc"];

            if (a != null)
            {
                if (a.userType < 2)
                    Response.Redirect("Default.aspx");
            }
            else
                Response.Redirect("SignIn.aspx");
        }

        protected void btnCreateStaff_Click(object sender, EventArgs e)
        {
            if ((!string.IsNullOrEmpty(tbxFirstName.Text)) && (!string.IsNullOrEmpty(tbxLastName.Text))
               && (!string.IsNullOrEmpty(tbxNRIC.Text)) && (!string.IsNullOrEmpty(tbxDOB.Text))
               && (!string.IsNullOrEmpty(tbxBankNo.Text)) && (!string.IsNullOrEmpty(tbxHomeAddress.Text))
               && (!string.IsNullOrEmpty(tbxPhoneNo.Text)) && (!string.IsNullOrEmpty(tbxEmail.Text)))
            {
                if (!StaffDBManager.checkDuplicateStaff(tbxNRIC.Text))
                {
                    if (ValidateDate(tbxDOB.Text))
                    {
                        if (StaffDBManager.createStaff(tbxFirstName.Text, tbxLastName.Text, tbxNRIC.Text, DateTime.Parse(tbxDOB.Text), tbxBankNo.Text, tbxHomeAddress.Text, tbxPhoneNo.Text, tbxEmail.Text, int.Parse(ddlDutyType.SelectedItem.Value)))
                        {
                            ClientScriptMgr.showSuccessBox("Staff has been successfully created!");
                            tbxFirstName.Text = "";
                            tbxLastName.Text = "";
                            tbxNRIC.Text = "";
                            tbxDOB.Text = "";
                            tbxBankNo.Text = "";
                            tbxHomeAddress.Text = "";
                            tbxPhoneNo.Text = "";
                            tbxEmail.Text = "";
                            ddlDutyType.SelectedIndex = 1;
                        }
                        else
                        {
                            ClientScriptMgr.showErrorBox("Unable to create staff, please try again later!");
                        }
                    }
                    else
                    {
                        ClientScriptMgr.showErrorBox("Invalid date of birth! Please use this format: 31-05-1967");
                    }
                }
                else
                {
                    ClientScriptMgr.showErrorBox("NRIC already exists!");
                }
            }
            else
            {
                ClientScriptMgr.showErrorBox("One or more required fields are empty!");
            }
        }

        private bool ValidateDate(string stringDateValue)
        {
            try
            {
                CultureInfo CultureInfoDateCulture = new CultureInfo("fr-FR");
                DateTime d = DateTime.ParseExact(stringDateValue, "dd-MM-yyyy", CultureInfoDateCulture);
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
}